package com.test.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;

/**
 * Created by jl on 2018/8/28 0028
 */
public class Demo {
    private static final String storePath = "C:\\Stash\\jyds-parent\\jyds-persistence\\target\\classes\\indexstore";

    private static void createIndex() throws IOException {
        Directory directory = FSDirectory.open(new File(storePath));

        Analyzer analyzer = new StandardAnalyzer();

        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);

        IndexWriter indexWriter = new IndexWriter(directory, config);

        String sourceFileName = Demo.class.getClassLoader().getResource("searchsource").getFile();
        File file = new File(sourceFileName);
        for (File f : file.listFiles()) {
            Document document = new Document();
            Field nameField = new TextField("fileName", f.getName(), Field.Store.YES);
            Field sizeField = new LongField("fileSize", 100, Field.Store.YES);
            Field contentField = new TextField("fileContent", "good morning !\n你好我好大家好", Field.Store.YES);

            document.add(nameField);
            document.add(sizeField);
            document.add(contentField);

            indexWriter.addDocument(document);
        }
        indexWriter.close();
    }

    private static void testMatchAllDocsQuery() throws IOException {
        Directory directory = FSDirectory.open(new File(storePath));
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);

        Query query = new MatchAllDocsQuery();

        TopDocs topDocs = indexSearcher.search(query, 10);
        //查询结果的总条数
        System.out.println("查询结果的总条数："+ topDocs.totalHits);
        //遍历查询结果
        //topDocs.scoreDocs存储了document对象的id
        //ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            //scoreDoc.doc属性就是document对象的id
            //int doc = scoreDoc.doc;
            //根据document的id找到document对象
            Document document = indexSearcher.doc(scoreDoc.doc);
            //文件名称
            System.out.println(document.get("fileName"));
            //文件内容
            System.out.println(document.get("fileContent"));
            //文件大小
            System.out.println(document.get("fileSize"));
            System.out.println("----------------------------------");
        }
        //关闭indexreader对象
        indexReader.close();
    }

    private void testSearchIndex() {

    }

    public static void main(String[] args) throws IOException {
//        createIndex();
        testMatchAllDocsQuery();
    }
}
